package com.hnchances.grade.service.impl;


import cn.hutool.crypto.SecureUtil;
import com.hnchances.grade.common.utils.JWTUtils;
import com.hnchances.grade.common.utils.R;
import com.hnchances.grade.entity.ClassEntity;
import com.hnchances.grade.entity.CourseEntity;
import com.hnchances.grade.entity.StudentEntity;
import com.hnchances.grade.service.ClassService;
import com.hnchances.grade.service.CourseService;
import com.hnchances.grade.service.StudentService;
import com.hnchances.grade.vo.RegisterVo;
import com.hnchances.grade.vo.UpdateUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnchances.grade.common.utils.PageUtils;
import com.hnchances.grade.common.utils.Query;
import com.hnchances.grade.dao.UserDao;
import com.hnchances.grade.entity.UserEntity;
import com.hnchances.grade.service.UserService;



@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
    @Autowired
    private CourseService courseService;

    @Autowired
    private ClassService classService;

    @Autowired
    private StudentService studentService;


    /**
     * 分页查询
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 进行注册
     * @param registerVo
     * @throws Exception
     */
    @Override
    public void registe(RegisterVo registerVo) throws Exception {
        //保存注册时应填的信息
        UserEntity userEntity = new UserEntity();

        BeanUtils.copyProperties(registerVo,userEntity);
        userEntity.setPassWord(SecureUtil.md5(registerVo.getPassWord()));
        //老师注册需要填写的
        //保存其他表内的信息courseId classId
        //根据course 和class查询然后赋值
        if (registerVo.getIdentity() == 1){
            ClassEntity classEntity = classService.getOne(
                    new QueryWrapper<ClassEntity>().eq("className", registerVo.getClassName()));
            CourseEntity courseEntity = courseService.getOne(
                    new QueryWrapper<CourseEntity>().eq("courseName", registerVo.getCourseName()));
            userEntity.setClassId(classEntity.getId());
            userEntity.setCourseId(courseEntity.getId());
        }
        //学生老师注册都要填写
        //保存自动填充的信息
        userEntity.setCreatDateTime(new Date());
        userEntity.setLastLoginDateTime(new Date());
        InetAddress addr = InetAddress.getLocalHost();
        String s = addr.toString();
        String[] split = s.split("/");
        userEntity.setLoginIp(split[1]);
        userEntity.setUpdateDateTime(new Date());
        userEntity.setVerify(1);
        userEntity.setStatus(1);
        this.save(userEntity);
    }

    /**
     * 根据id删除用户
     * 并且是本班级的学生
     *
     * @param id
     * @return
     */
    @Override
    public R removeByIdConditions(Integer id, String token) {
        String username = JWTUtils.getUsername(token);
        UserEntity user = this.getOne(new QueryWrapper<UserEntity>().eq("userName", username));
        UserEntity byId = this.getById(id);
        if (byId.getIdentity() == 1 || byId.getIdentity() == 2){
            //是老师或者管理不可以进行删除
            return R.error("您不能进行删除,请联系管理员");
        }
        Integer classId = user.getClassId();
        StudentEntity student = studentService.getById(id);
        if (student.getClassId() == classId){
            this.removeById(id);
            return R.ok();
        }else {
            return R.error("当前学生不是您的学生，您不可以进行删除");
        }

    }

    /**
     * 修改基本信息以及需要连表查询的信息
     * @param updateUserVo
     * @return
     */
    @Override
    public R updateByIdDetail(UpdateUserVo updateUserVo) {
        UserEntity user = new UserEntity();
        if (updateUserVo.getIdentity() == 2){
            ClassEntity classEntity = classService.getOne(
                    new QueryWrapper<ClassEntity>().eq("className", updateUserVo.getClassName()));
            CourseEntity courseEntity = courseService.getOne(
                    new QueryWrapper<CourseEntity>().eq("courseName", updateUserVo.getCourseName()));
            if (courseEntity == null || classEntity == null){
                return R.error("请填写正确的班级名或者课程名");
            }
            user.setClassId(classEntity.getId());
            user.setCourseId(courseEntity.getId());
        }
        BeanUtils.copyProperties(updateUserVo, user);
        user.setPassWord(SecureUtil.md5(user.getPassWord()));
        this.updateById(user);
        return R.ok();
    }

    /**
     * 返回除了密码的所有字段值
     * @return
     */
    @Override
    public List<UserEntity> listWithoutPassword() {
        List<UserEntity> userEntities = this.baseMapper.selectWithoutPassword();
        return userEntities;
    }


}